package com.zhuoyue.biz.core.api.service.impl;

import com.zhuoyue.biz.core.api.domain.DeviceAndGatewayDO;
import com.zhuoyue.biz.core.api.domain.ZigbeeDeviceProperty;
import com.zhuoyue.biz.core.api.dto.req.ControlReq;
import com.zhuoyue.biz.core.api.service.impl.device.*;
import com.zhuoyue.biz.core.command.cmd.common.faa.base.*;
import com.zhuoyue.biz.core.command.cmd.common.faa.*;
import com.zhuoyue.biz.core.command.cmd.common.fab.base.ManyWayBaseCmd;
import com.zhuoyue.biz.core.command.cmd.common.fac.*;
import com.zhuoyue.biz.core.command.cmd.common.fad.AirConditioningGateway;
import com.zhuoyue.biz.core.command.cmd.common.fbe.HumanInfrared;
import com.zhuoyue.biz.core.command.cmd.common.fc8.base.BaseCmd0Fc8;
import com.zhuoyue.biz.core.command.cmd.common.fe6.*;
import com.zhuoyue.biz.core.command.service.CmdSqlHandleService;
import com.zhuoyue.common.constant.DeviceTypeConstant;

import com.zhuoyue.common.exception.ErrorType;
import com.zhuoyue.common.exception.ZyServerException;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.jetbrains.annotations.NotNull;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhuoyue.biz.core.api.dao.OdStructureMapper;
import com.zhuoyue.biz.core.api.domain.OdStructure;
import com.zhuoyue.biz.core.api.service.OdStructureService;

import java.io.UnsupportedEncodingException;
import java.util.List;


/**
 * @author wjming
 */
@SuppressWarnings("DuplicateBranchesInSwitch")
@Service
@RequiredArgsConstructor(onConstructor = @__(@Autowired))
public class OdStructureServiceImpl extends ServiceImpl<OdStructureMapper, OdStructure> implements OdStructureService {
    private final CmdSqlHandleService cmdSqlHandleService;
    private final TwoColorTemperatureCommon colorTemperatureCommon;
    private final OneColorTemperatureCommon oneColorTemperatureCommon;
    private final SwitchControllerCommon switchControllerCommon;
    private final FingerprintLockCommon fingerprintLockCommon;
    private final ZhuoYueMainBanCommon zhuoYueMainBanCommon;
    private final DeviceTaskCommon deviceTaskCommon;

    /**
     * 定时任务修改状态
     */
    private final String SET_TIMED_TASK_STATUS = "SetTimedTaskStatus";
    /**
     * 删除定时任务
     */
    private final String DELETE_TIMED_TASK = "DeleteTimedTask";
    /**
     * 设置定时任务
     */
    private final String SET_TIMED_TASK = "SetTimedTask";

    @Override
    public synchronized String structureCmd(@NotNull DeviceAndGatewayDO deviceAndGatewayDO, @NotNull ControlReq controlReq) {
        String cmd;
        String odIndex = deviceAndGatewayDO.getOdIndex();
        String deviceType = deviceAndGatewayDO.getDeviceType();
        String productType = StringUtils.substring(deviceAndGatewayDO.getProductType(), 0, 2);
        String way = StringUtils.substringAfter(deviceAndGatewayDO.getProductType(), "_");
        way = StringUtils.isEmpty(way) ? "1" : way;
        String device = odIndex + deviceType + productType;
        switch (odIndex) {
            case "0FAA":
                cmd = device0Faa(device, way, deviceAndGatewayDO, controlReq);
                break;
            //多路开关
            case "0FAB":
                cmd = device0Fab(device, way, deviceAndGatewayDO, controlReq);
                break;
            //指纹锁
            case "0FBF":
                cmd = device0Fbf(device, deviceAndGatewayDO, controlReq);
                break;
            //空调、地暖面板
            case "0FAC":
                cmd = device0Fac(device, way, deviceAndGatewayDO, controlReq);
                break;
            case "0FAD":
                cmd = device0Fad(device, deviceAndGatewayDO, controlReq);
                break;
            //计量插座
            case "0FC8":
                cmd = device0Fc8(device, deviceAndGatewayDO, controlReq);
                break;
            //透传转发
            case "0FE6":
                cmd = device0Fe6(device, deviceAndGatewayDO, controlReq);
                break;
            case "0FBE":
                cmd = device0Fbe0402(device, deviceAndGatewayDO, controlReq);
                break;
            default:
                cmd = "";
                break;
        }
        return cmd;
    }

    private String device0Fbe0402(String device, DeviceAndGatewayDO deviceAndGatewayDO, ControlReq controlReq) {
        String cmd = "";
        switch (device) {
            case DeviceTypeConstant.HUMAN_INFRARED:
                cmd = HumanInfrared.setControl(deviceAndGatewayDO, controlReq);
                break;
            default:
                break;
        }
        return cmd;
    }

    /**
     * 空调群控
     *
     * @param device
     * @param deviceAndGatewayDO
     * @param controlReq
     * @return
     */
    private String device0Fad(final String device, DeviceAndGatewayDO deviceAndGatewayDO, ControlReq controlReq) {
        String cmd = "";
        switch (device) {
            case DeviceTypeConstant.AIR_CONDITIONING_GATEWAY:
                cmd = AirConditioningGateway.control(deviceAndGatewayDO, controlReq);
                break;
            default:
                break;
        }
        return cmd;
    }


    private String device0Faa(String device, String way, DeviceAndGatewayDO deviceAndGatewayDO, ControlReq controlReq) {
        String cmd = "";
        switch (device) {
            //强电控制盒
            case DeviceTypeConstant.STRONG_CURRENT_CONTROL_BOX:
                //弱电控制盒
            case DeviceTypeConstant.WEAK_CURRENT_CONTROL_BOX:
                cmdSqlHandleService.modifyOneNightLightTime(deviceAndGatewayDO.getId(), controlReq.getActions());
                cmd = BaseCmd.control(deviceAndGatewayDO, controlReq, way);
                break;
            //投影架
            case DeviceTypeConstant.PROJECTION_FRAME:
                //推拉开窗器
            case DeviceTypeConstant.TL_WINDOW_OPENER:
                //平推开窗器
            case DeviceTypeConstant.PY_WINDOW_OPENER:
                //机械手
            case DeviceTypeConstant.MANIPULATOR_CONTROLLER:
                //一路面板
            case DeviceTypeConstant.ONE_ROAD_PANEL:
                //一路联控面板
            case DeviceTypeConstant.ONE_ROAD_JOINT_CONTROL_PANEL:
                //86开关插座（不带计量）
            case DeviceTypeConstant.SWITCH_SOCKET_86:
                //移动开关插座（不带计量）
            case DeviceTypeConstant.MOBILE_SWITCH_SOCKET:
                //二路面板
            case DeviceTypeConstant.TWO_ROADS_PANEL:
                //二路联控面板
            case DeviceTypeConstant.TWO_ROADS_JOINT_CONTROL_PANEL:
                //三路面板
            case DeviceTypeConstant.THREE_ROADS_PANEL:
                //三路联控面板
            case DeviceTypeConstant.THREE_ROADS_JOINT_CONTROL_PANEL:
                //四路联控面板
            case DeviceTypeConstant.FOUR_ROADS_JOIN_CONTROL_PANEL:
                cmd = BaseCmd.control(deviceAndGatewayDO, controlReq, way);
                break;
            //多彩球泡灯/灯带控制盒
            case DeviceTypeConstant.COLORFUL_BULB_LAMP:
                ZigbeeDeviceProperty zigbeeDeviceProperty = cmdSqlHandleService.getDeviceProperty(deviceAndGatewayDO.getId());
                cmd = ColorfulBulbLamp0Faa0B02.control(deviceAndGatewayDO, controlReq, zigbeeDeviceProperty);
                break;
            //灯带控制器2.0
            case DeviceTypeConstant.COLORFUL_BULB_LAMP_TWO:
                cmd = ColorfulBulbLampTwo0Faa0B03.control(deviceAndGatewayDO, controlReq, false);
                break;
            //晾衣架
            case DeviceTypeConstant.CLOTHES_HANGER:
                cmd = ClothesHanger0Faa0C02.control(deviceAndGatewayDO, controlReq);
                break;
            //双色温
            case DeviceTypeConstant.TWO_COLOR_TEMPERATURE:
                cmd = colorTemperatureCommon.twoColorTemperatureDeal(deviceAndGatewayDO, controlReq);
                break;

            //单色温
            case DeviceTypeConstant.ONE_COLOR_TEMPERATURE:
                cmd = oneColorTemperatureCommon.oneColorTemperatureDeal(deviceAndGatewayDO, controlReq);
                break;

            //开关控制盒
            case DeviceTypeConstant.SWITCH_CONTROLLER:
                cmd = switchControllerCommon.switchControllerDeal(deviceAndGatewayDO, controlReq);
                break;

            //插卡取电
            case DeviceTypeConstant.PLUG_IN_CARD_GET_POWER:
                cmd = PlugInCardGetPower0Faa8106.set(deviceAndGatewayDO, controlReq);
                break;
            //场景面板
            case DeviceTypeConstant.SCENE_PANEL:
                String code = cmdSqlHandleService.getSceneCode(controlReq.getActions().get("sceneId") + "");
                cmd = ScenePanel0Faa8A02.set(deviceAndGatewayDO, controlReq, code);
                break;
            //液晶场景面板控制器
            case DeviceTypeConstant.LCD_SCENE_CONTROLLER:
                try {
                    List<String> codeAndName = cmdSqlHandleService.getSceneCodeAndName(controlReq.getActions().get("sceneId") + "");
                    cmd = LcdSceneController0Faa8A03.set(deviceAndGatewayDO, controlReq, codeAndName);
                } catch (UnsupportedEncodingException e) {
                    throw new ZyServerException(ErrorType.Service_Fail.getCode(), "编码异常,请检查场景名称是否出错");
                } catch (Exception e) {
                    throw new ZyServerException(ErrorType.Service_Fail.getCode(), "场景配置系统异常");
                }
                break;
            //智能场景面板(U系列场景面板)
            case DeviceTypeConstant.INTELLIGENT_SCENE_CONTROLLER:
                String codeStr = cmdSqlHandleService.getSceneCode(controlReq.getActions().get("sceneId") + "");
                cmd = IntelligentSceneController0Faa8A04.set(deviceAndGatewayDO, controlReq, codeStr);
                break;
            //场景面板米提尔(带雷达功能)
            case DeviceTypeConstant.SIX_ROAD_SCENE_PANEL_MTE:
                String code3 = cmdSqlHandleService.getSceneCode(controlReq.getActions().get("sceneId") + "");
                cmd = SixRoadScenePanelMte0Faa8A05.set(deviceAndGatewayDO, controlReq, code3);
                break;
            //四路场景面板极光版
            case DeviceTypeConstant.FOUR_ROADS_SCENE_PANEL:
                //魔幻场景
            case DeviceTypeConstant.MAGIC_SCENE:
                //随意贴
            case DeviceTypeConstant.POST_AT_WILL:
                String code5 = cmdSqlHandleService.getSceneCode(controlReq.getActions().get("sceneId") + "");
                cmd = BaseSetCmd.set(deviceAndGatewayDO, controlReq, code5);
                break;
            //多功能开关
            case DeviceTypeConstant.MULTI_FUNCTION_SWITCH:
            case DeviceTypeConstant.ZHUO_YUE_MIAN_BAN:
                cmd = zhuoYueMainBanCommon.zhuoYueMainBanDeal(way, deviceAndGatewayDO, controlReq);
                break;
            //六路场景面板
            case DeviceTypeConstant.SIX_ROAD_SCENE_PANEL:
                String code7 = cmdSqlHandleService.getSceneCode(controlReq.getActions().get("sceneId") + "");
                cmd = BaseSetCmd.set(deviceAndGatewayDO, controlReq, code7);
                break;
            //静音窗帘zigbee版0FAA0E02,电动卷帘
            case DeviceTypeConstant.SILENT_CURTAIN:
                cmd = SilentCurtain0Faa0E02.control(deviceAndGatewayDO, controlReq, false);
                break;
            //光速语音控制盒
            case DeviceTypeConstant.SOL_VOICE:
                String controlType = controlReq.getControlType();
                //若是定时任务修改状态，先从数据库中获取控制参数
                if (SET_TIMED_TASK_STATUS.equals(controlType) || DELETE_TIMED_TASK.equals(controlType)) {
                    controlReq = deviceTaskCommon.getControlParams(deviceAndGatewayDO, controlReq);
                }
                cmd = SolVoice0Faa8a11.control(deviceAndGatewayDO, controlReq);
                //若是定时任务，将数据保存到redis
                if (SET_TIMED_TASK.equals(controlType) || SET_TIMED_TASK_STATUS.equals(controlType) || DELETE_TIMED_TASK.equals(controlType)) {
                    deviceTaskCommon.saveToRedis(controlReq, cmd, 5);
                }
                break;
            default:
                break;
        }
        return cmd;
    }

    private String device0Fab(String device, String way, DeviceAndGatewayDO deviceAndGatewayDO, ControlReq controlReq) {
        String cmd = "";
        switch (device) {
            //八路控制器
            case DeviceTypeConstant.EIGHT_ROUTES_CONTROLLER:
                cmd = ManyWayBaseCmd.control(deviceAndGatewayDO, controlReq, way);
                break;
            //四路面板
            case DeviceTypeConstant.FOUR_ROADS_PANEL_L:
                cmd = ManyWayBaseCmd.control(deviceAndGatewayDO, controlReq, way);
                break;

            default:
                break;
        }
        return cmd;
    }

    private String device0Fe6(String device, DeviceAndGatewayDO deviceAndGatewayDO, ControlReq controlReq) {
        String cmd = "";
        switch (device) {
            //红外学习仪
            case DeviceTypeConstant.INFRARED_LEARNING_INSTRUMENT:
                String controlType = controlReq.getControlType();

                //若是定时任务修改状态，先从数据库中获取控制参数
                if (SET_TIMED_TASK_STATUS.equals(controlType) || DELETE_TIMED_TASK.equals(controlType)) {
                    controlReq = deviceTaskCommon.getControlParams(deviceAndGatewayDO, controlReq);
                }

                cmd = HongWaiXueXiYi0Fe60202.control(deviceAndGatewayDO, controlReq);

                //若是定时任务，将数据保存到redis
                if (SET_TIMED_TASK.equals(controlType) || SET_TIMED_TASK_STATUS.equals(controlType) || DELETE_TIMED_TASK.equals(controlType)) {
                    deviceTaskCommon.saveToRedis(controlReq, cmd, 5);
                }
                break;
            //背景音乐
            case DeviceTypeConstant.MUSIC_BACKGROUND_DEVICE_WALTZ:
                cmd = BeiJingYinYueFhwise0Fe60203.control(deviceAndGatewayDO, controlReq);
                break;
            //取暖器
            case DeviceTypeConstant.ROOM_HEATER_HCS:
                cmd = QuNuqanQi0Fe60206.control(deviceAndGatewayDO, controlReq);
                break;
            //静音窗帘
            case DeviceTypeConstant.QUIET_CURTAIN_BS:
                cmd = JingYinChuangLian0Fe60209.control(deviceAndGatewayDO, controlReq);
                break;
            //智能窗帘
            case DeviceTypeConstant.TRANSLATION_WINDOW_OPENER:
                //平移开窗器
            case DeviceTypeConstant.SMART_CURTAIN:
                cmd = ZhiNengChuangLian0Fe60210.control(deviceAndGatewayDO, controlReq);
                break;
            default:
                break;

        }
        return cmd;
    }

    private String device0Fc8(String device, DeviceAndGatewayDO deviceAndGatewayDO, ControlReq controlReq) {
        String cmd = "";
        switch (device) {
            //10A计量插座
            case DeviceTypeConstant.METERING_SOCKET_10_A:
                cmd = BaseCmd0Fc8.control(deviceAndGatewayDO, controlReq);
                break;
            //16A计量插座
            case DeviceTypeConstant.METERING_SOCKET_16_A:
                cmd = BaseCmd0Fc8.control(deviceAndGatewayDO, controlReq);
                break;

            default:
                break;
        }
        return cmd;
    }

    private String device0Fac(String device, String way, DeviceAndGatewayDO deviceAndGatewayDo, ControlReq controlReq) {
        String cmd = "";
        switch (device) {
            //空调
            case DeviceTypeConstant.AIR_CONDITIONING_PANEL:
                cmd = KongTiaoMianBan0Fac0202.control(deviceAndGatewayDo, controlReq);
                break;
            //地暖
            case DeviceTypeConstant.FLOOR_HEATING_PANEL:
                cmd = DiNuanMianBan0Fac0302.control(deviceAndGatewayDo, controlReq);
                break;
            //智能空调面板
            case DeviceTypeConstant.SMART_AIR_CONDITION_PANEL:
                cmd = ZhiNengKongTiaoMianBan0Fac0502.control(deviceAndGatewayDo, controlReq, Integer.parseInt(way), false);
                break;
            //空调伴侣
            case DeviceTypeConstant.SMART_AIR_PARTNER:
                String controlType = controlReq.getControlType();

                //若是定时任务修改状态，先从数据库中获取控制参数
                if (SET_TIMED_TASK_STATUS.equals(controlType) || DELETE_TIMED_TASK.equals(controlType)) {
                    controlReq = deviceTaskCommon.getControlParams(deviceAndGatewayDo, controlReq);
                }
                cmd = SmartAirPartner0Fac0503.control(deviceAndGatewayDo, controlReq);
                //若是定时任务，将数据保存到redis
                if (SET_TIMED_TASK.equals(controlType) || SET_TIMED_TASK_STATUS.equals(controlType) || DELETE_TIMED_TASK.equals(controlType)) {
                    deviceTaskCommon.saveToRedis(controlReq, cmd, 5);
                }
            default:
                break;

        }
        return cmd;
    }

    private String device0Fbf(String device, DeviceAndGatewayDO deviceAndGatewayDO, ControlReq controlReq) {
        String cmd = "";
        //指纹锁
        if (DeviceTypeConstant.FINGERPRINT_LOCK.equals(device)) {
            cmd = fingerprintLockCommon.fingerDeal(deviceAndGatewayDO, controlReq);
        }
        return cmd;
    }
}
